package com.wei.czz.framework.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.vo.common.log.LogFormVo;
import com.wei.czz.common.vo.common.log.LogStatsVo;
import com.wei.czz.framework.common.dao.LogDao;
import com.wei.czz.framework.common.entity.LogEntity;
import com.wei.czz.framework.common.service.LogService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2021-05-24 15:34:30
 * className: LogController 系统日志操作请求服务接口实现类
 * version: 1.0
 * description:
 */
@Service("logService")
@AllArgsConstructor
public class LogServiceImpl extends ServiceImpl<LogDao, LogEntity> implements LogService {

    private static final Logger log = LoggerFactory.getLogger(LogServiceImpl.class);

    @Override
    public void saveOrEdit(LogEntity _log) {
        Long id = _log.getId();
        if (Objects.isNull(id)) {
            int count = baseMapper.insert(_log);
            log.info("插入请求日志完成。count={}", count);

        } else {

            int count = baseMapper.updateById(_log);
            log.info("修改请求日志完成。count={}", count);
        }
    }

    @Override
    public List<LogEntity> findList(LogStatsVo logStatsVo) {
        LambdaQueryWrapper<LogEntity> logLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 设置查询字段
        logLambdaQueryWrapper.select(LogEntity::getUri, LogEntity::getCode, LogEntity::getStartTime,
                LogEntity::getEndTime);
        // 参数匹配
        logLambdaQueryWrapper.ge(LogEntity::getStartTime, logStatsVo.getStartTime())
                .eq(StringUtils.isNotBlank(logStatsVo.getUrl()), LogEntity::getUri, logStatsVo.getUrl());
        // 查询数据
        List<LogEntity> logList = baseMapper.selectList(logLambdaQueryWrapper);
        log.info("查询用于统计的请求日志完成。list.size={}", logList.size());
        return logList;
    }

    @Override
    public PageDto<LogEntity> getPageList(LogFormVo logFormVo) {

        // mp分页对象
        Page<LogEntity> page = new Page<>(logFormVo.getPage(), logFormVo.getLimit());

        LambdaQueryWrapper<LogEntity> logLambdaQueryWrapper = new LambdaQueryWrapper<>();
        logLambdaQueryWrapper.ge(LogEntity::getStartTime, logFormVo.getRequestStartTime())
                .le(LogEntity::getEndTime, logFormVo.getRequestEndTime())
                .eq(Objects.nonNull(logFormVo.getUserId()), LogEntity::getUserId, logFormVo.getUserId())
                .eq(StringUtils.isNotBlank(logFormVo.getIp()), LogEntity::getIp, logFormVo.getIp())
                .likeRight(StringUtils.isNotBlank(logFormVo.getAddress()), LogEntity::getAddress, logFormVo.getAddress())
                .likeRight(StringUtils.isNotBlank(logFormVo.getUri()), LogEntity::getUri, logFormVo.getUri())
                .eq(StringUtils.isNotBlank(logFormVo.getType()), LogEntity::getType, logFormVo.getType())
                .eq(StringUtils.isNotBlank(logFormVo.getClassName()), LogEntity::getClassName, logFormVo.getClassName())
                .eq(StringUtils.isNotBlank(logFormVo.getMethod()), LogEntity::getMethod, logFormVo.getMethod())
                .eq(Objects.nonNull(logFormVo.getCode()), LogEntity::getCode, logFormVo.getCode());
        // 设置排序方式
        logLambdaQueryWrapper.orderByDesc(LogEntity::getStartTime);
        // 分页查询
        baseMapper.selectPage(page, logLambdaQueryWrapper);
        log.info("分页查询请求日志完成。list.size={} totalNum={}", page.getRecords().size(), page.getTotal());

        return new PageDto<>(page);
    }

    @Override
    public LogEntity findUriLastSuccessRequest(String uri) {

        LambdaQueryWrapper<LogEntity> logLambdaQueryWrapper = new LambdaQueryWrapper<>();
        logLambdaQueryWrapper.eq(LogEntity::getUri, uri);
        logLambdaQueryWrapper.orderByDesc(LogEntity::getStartTime);
        logLambdaQueryWrapper.last("limit 1");
        // 查询
        List<LogEntity> logList = baseMapper.selectList(logLambdaQueryWrapper);
        log.info("查询接口最后一次请求成功日志完成。list.size={}", logList.size());
        return logList.get(0);
    }

}